package com.shelpe.services.algorithm.mobileQscore;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import com.shelpe.services.algorithm.configuration.MobileQscoreProperties;
import com.shelpe.services.algorithm.constant.MobileRank;
import com.shelpe.services.algorithm.entity.jpa.DevelopmentKeyword;
import com.shelpe.services.algorithm.sustain.SustainRule;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@EnableConfigurationProperties(MobileQscoreProperties.class)
public class MobileQscoreFirstRunKeywordSustainRule implements SustainRule<MobileScoreKeywordSustainer> {

	@Autowired
	private MobileQscoreProperties prop;
	
	@Override
	public boolean check(MobileScoreKeywordSustainer entity) {
		long days = ((new Date()).getTime() - entity.getAdgroup().getCreateTime().getTime()) / (1000 * 60 * 60 *24);
		boolean firstRun = (days < this.prop.getDevelopmentDays() && entity.getAdgroup().getAdjustCount() == 0);
		log.debug("{} first runing: {}", entity.getKeyword());
		return firstRun && entity.getKeyword() != null;
	}

	@Override
	public boolean prepare(MobileScoreKeywordSustainer entity) {
		log.debug("prepare MobileQscoreFirstRunKeywordSustainRule: {}", entity);
		return true;
	}

	@Override
	public SustainRule.SustainStatus sustain(MobileScoreKeywordSustainer entity) {
		SustainRule.SustainStatus ss = SustainRule.SustainStatus.keep;
		
		int mobileQscore = entity.getKeyword().getWirelessQscore() != null?entity.getKeyword().getWirelessQscore().intValue():-1;
		log.debug("keyword({} - {}) mobile qscore: {}", entity.getKeyword().getKeywordId(), entity.getKeyword().getWord(), mobileQscore);
		if(mobileQscore == -1){
			log.warn("no mobile qscore keyword({} - {}): {}", entity.getKeyword().getKeywordId(), entity.getKeyword().getWord());
		}
		
		//低质量得分, 0可能为质量分未同步
		if(mobileQscore > 0 && mobileQscore < prop.getLowMobileQscore()){
			ss = SustainRule.SustainStatus.remove;
			entity.getSnapshot().setReason("low_mobile_qscore[" + mobileQscore + "]");
		}
		
		int rank = MobileRank.from16to20OfIndex.getValue();
		
		if(ss == SustainRule.SustainStatus.keep){
			if(mobileQscore >= prop.getHighMobileScore()){
				ss = SustainRule.SustainStatus.ranking;
				rank = MobileRank.from16to20OfIndex.getValue();
			}
		}
		
		if(ss == SustainRule.SustainStatus.ranking){
			DevelopmentKeyword dev = new DevelopmentKeyword();
			dev.setKeywordID(entity.getKeyword().getKeywordId());
			dev.setWord(entity.getKeyword().getWord());
			dev.setOriginalValue(entity.getKeyword().getMaxMobilePrice());
			dev.setTargetType(DevelopmentKeyword.TargetType.range20);
			dev.setTarget(rank);
			dev.setStatus(DevelopmentKeyword.Status.executable);
			dev.setCreatedTime(new Date());
			dev.setModifiedTime(new Date());
			if(entity.getRealtimeRank() != null){
				dev.setLastRank(entity.getRealtimeRank().getMoibleRank());
			}
			entity.setDevelopmentKeyword(dev);
		}
		return ss;
	}

	@Override
	public boolean maintain(MobileScoreKeywordSustainer entity, SustainRule.SustainStatus status) {
		return true;
	}

	@Override
	public boolean mark(MobileScoreKeywordSustainer entity) {
		return true;
	}

}
